package cn.knight.org.controller;

import cn.knight.basic.annotation.PreAuthorize;
import cn.knight.basic.utils.AjaxResult;
import cn.knight.basic.utils.PageList;
import cn.knight.org.domain.Employee;
import cn.knight.org.query.EmployeeQuery;
import cn.knight.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
    * @Title: loadAll
    * @Description: 部门查询所有数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  24/6/2022 上午11:55
    * @Parameters: []
    * @Return java.util.List<cn.knight.org.domain.Employee>
    */
    @GetMapping
    @ApiOperation(value = "查询部门所有数据" )
    @PreAuthorize(name = "员工查询", sn = "employee:loadAll")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
    * @Title: loadById
    * @Description: 根据部门主键id查询数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  24/6/2022 上午11:57
    * @Parameters: [id]
    * @Return cn.knight.org.domain.Employee
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public  Employee loadById(@ApiParam(name="ID属性",required = true)@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
    * @Title: remove
    * @Description: 根据部门主键id删除数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  24/6/2022 上午11:57
    * @Parameters: [id]
    * @Return cn.knight.org.domain.Employee
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "员工删除", sn = "employee:remove")
    public  AjaxResult remove(@ApiParam(value="ID属性",required = true)@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！！");
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 新增或修改数据
    * @Author: winnie
    * @Version: 1.0
    * @Date:  24/6/2022 下午12:02
    * @Parameters: [employee]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PutMapping
    @ApiOperation(value = "新增/修改" )
    @PreAuthorize(name = "员工新增/修改", sn = "employee:addOrUpt")
    public AjaxResult addOrUpt(@ApiParam(name="部门对象",required = true)@RequestBody Employee employee){
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: queryRows
    * @Description: 分页查询+高级查询
    * @Author: winnie
    * @Version: 1.0
    * @Date:  24/6/2022 下午12:11
    * @Parameters: [employeeQuery]
    * @Return cn.knight.basic.utils.PageList<cn.knight.org.domain.Employee>
    */
    @PostMapping
    @ApiOperation(value = "分页查询+高级查询" )
    @PreAuthorize(name = "员工列表", sn = "employee:queryRows")
    public PageList<Employee> queryRows(@ApiParam(name="部门对象",required = true)@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> queryRows = employeeService.queryRows(employeeQuery);
        return queryRows;
    }

    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: winnie
    * @Version: 1.0
    * @Date:  26/6/2022 下午11:30
    * @Parameters: [ids]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PatchMapping
    @PreAuthorize(name = "员工批量删除", sn = "employee:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }


}
